package com.example.gkcircle.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.LogInCallback;
import com.avos.avoscloud.SignUpCallback;
import com.avos.avoscloud.UpdatePasswordCallback;
import com.example.gkcircle.BaseActivity;
import com.example.gkcircle.R;
import com.example.gkcircle.guide.GuideActivity;
import com.example.gkcircle.utils.MyAlertView;
import com.example.gkcircle.utils.ToastFactory;
import com.example.gkcircle.widget.EditTextWithDelete;

/**
 * Created by Jehu on 16/4/11.
 */
public class ResetPasswordActivity extends BaseActivity implements View.OnClickListener{

    private EditTextWithDelete mOldEdit;
    private EditTextWithDelete mNewEdit;
    private TextView mTitle;
    private ImageButton mBackBtn;
    private TextView  mFinishBtn;
    private View mDivider;

    //修改密码标识
    private final int RESET_PASSWORD_FLAG=0;
    //设置密码标识（注册和忘记密码用）
    private final int SET_PASSWORD_FLAG=1;


    @Override
    protected void initView() {
       setContentView(R.layout.reset_password_activity);
        mOldEdit=(EditTextWithDelete)this.findViewById(R.id.reset_password_old_edit);
        mNewEdit=(EditTextWithDelete)this.findViewById(R.id.reset_password_new_edit);
        mBackBtn=(ImageButton)this.findViewById(R.id.reset_password_back);
        mTitle=(TextView)this.findViewById(R.id.reset_password_title);
        mFinishBtn=(TextView)this.findViewById(R.id.reset_password_finish);
        mDivider=(View)this.findViewById(R.id.reset_password_divider);
    }

    @Override
    protected void initListener() {
        mBackBtn.setOnClickListener(this);
        mFinishBtn.setOnClickListener(this);
    }

    @Override
    protected void setData() {
        switch (getIntent().getExtras().getInt("flag")){
            //修改密码
            case RESET_PASSWORD_FLAG:
                mTitle.setText(getResources().getString(R.string.reset_password));
                break;
            //设置密码
            case SET_PASSWORD_FLAG:
                mTitle.setText(getResources().getString(R.string.set_password));
                mNewEdit.setVisibility(View.GONE);
                mDivider.setVisibility(View.GONE);
                mOldEdit.setHint("密码");

                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.reset_password_back:
                finish();
                break;
            case R.id.reset_password_finish:
                switch (getIntent().getExtras().getInt("flag")){
                    //修改密码
                    case RESET_PASSWORD_FLAG:
                        final String oldPsw=mOldEdit.getText().toString().trim();
                        final String newPsw=mNewEdit.getText().toString().trim();
                        final AVUser user=AVUser.getCurrentUser();
                        if(newPsw.toCharArray().length>16 || user==null){
                            return;
                        }
                        final String currentUsername =user.getUsername();
                        user.updatePasswordInBackground(oldPsw, newPsw, new UpdatePasswordCallback() {
                            @Override
                            public void done(AVException e) {
                                if (e == null) {
                                    ToastFactory.getToast(ResetPasswordActivity.this, "修改密码成功").show();
                                    ResetPasswordActivity.this.overridePendingTransition(0, R.anim.zoom_in);
                                    Intent intent = new Intent(ResetPasswordActivity.this, LoginActivity.class);
                                    Bundle bundle = new Bundle();
                                    bundle.putInt("guide", 3);
                                    bundle.putString("phone", currentUsername);
                                    bundle.putString("password", newPsw);
                                    intent.putExtras(bundle);
                                    ResetPasswordActivity.this.startActivity(intent);
                                    finish();
                                }
                            }
                        });
                        break;
                    //设置密码
                    case SET_PASSWORD_FLAG:
                        final String password=mOldEdit.getText().toString().trim();
                        if(password.toCharArray().length>16){
                            return;
                        }
                        final String phone=getIntent().getExtras().getString("phone");
                        AVUser registerUser=new AVUser();
                        registerUser.setUsername(phone);
                        registerUser.setPassword(password);
                        registerUser.signUpInBackground(new SignUpCallback() {
                            @Override
                            public void done(AVException e) {
                                if(e==null){
                                    AVUser.logInInBackground(phone, password, new LogInCallback<AVUser>() {
                                        @Override
                                        public void done(AVUser avUser, AVException e) {
                                            if(e==null){
                                                ResetPasswordActivity.this.overridePendingTransition(0, R.anim.zoom_in);
                                                Intent intent =new Intent(ResetPasswordActivity.this,LoginActivity.class);
                                                Bundle bundle=new Bundle();
                                                bundle.putInt("guide",3);
                                                bundle.putString("phone", phone);
                                                bundle.putString("password",password);
                                                intent.putExtras(bundle);
                                                GuideActivity.isFinishRegister=true;
                                                ResetPasswordActivity.this.startActivity(intent);
                                                finish();
                                            }
                                        }
                                    });
                                }else{
                                    final String status;
                                    switch (e.getCode()){
                                        case 202:
                                            status="该手机号码已经被注册";
                                            break;
                                        default:
                                            status=getResources().getString(R.string.connection_error);
                                            break;
                                    }
                                    MyAlertView.showAlertViewWithOK(ResetPasswordActivity.this, getString(R.string.register_failure), status, "好");
                                }
                            }
                        });
                        break;
                }
                break;
        }
    }
}
